package com.mercury.share.controller;


import com.mercury.share.entity.ShareComments;
import com.mercury.share.entity.vo.CommentInfoVo;
import com.mercury.share.entity.vo.ShareInfo;
import com.mercury.share.service.ShareCommentsService;
import com.mercury.share.service.ShareImgsService;
import com.mercury.share.service.ShareService;
import com.mercury.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author tong
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/share")
public class ShareController {

    @Autowired
    private ShareService shareService;

    @Autowired
    private ShareCommentsService commentsService;

    @Autowired
    private ShareImgsService imgsService;

    /**
     * @date 2021/11/22 9:17
     * @params null
     * @return
     * @description 获取所有的分享列表
    */
    @ApiOperation("获取所有的分享列表")
    @GetMapping("shareList")
    public R getShareList() {
        List<ShareInfo> shareList = shareService.getShareList();
        return R.ok().data("list",shareList);

    }


    @ApiOperation("获得分享的具体信息")
    @GetMapping("shareInfo/{id}")
    public R getShareInfo(@PathVariable int id){
        ShareInfo shareInfo = shareService.getShareInfo(id);
        shareInfo.setCount(commentsService.getCount(id));
        shareInfo.setPicList(imgsService.getList(id));
        List<CommentInfoVo> commentList = commentsService.getCommentList(id);
        return R.ok().data("shareInfo",shareInfo).data("commentList",commentList);

    }

    /**
     * @date 2021/11/22 9:20
     * @params null
     * @return
     * @description 添加一条新的咖啡分享
    */
    @ApiOperation("添加一条分享")
    @PostMapping("shareInfo")
    @Transactional
    public R addShare(@RequestBody ShareInfo shareInfo) {
        shareService.addShare(shareInfo);
        return R.ok();
    }

    /**
     * @date 2021/11/22 9:21
     * @params null
     * @return
     * @description 删除咖啡分享
    */

    @ApiOperation("删除咖啡分享")
    @DeleteMapping("/shareInfo/{id}")
    public R deleteShare(@PathVariable int id){
        boolean res = shareService.removeById(id);
        if(res) return R.ok().message("删除成功");
        else return R.error().message("删除失败");

    }

    /**
     * @date 2021/11/22 9:21
     * @params null
     * @return
     * @description 判断这一条分享是不是本人的
    */
    @ApiOperation("判断该条分享是本人的")
    @GetMapping("personal/{shareId}/{userId}")
    public R isPersonal(@PathVariable int shareId,
                        @PathVariable String userId) {
        Boolean is = shareService.isPersonal(shareId,userId);
        if(is) return R.ok().message("是本人的分享");
        else return R.ok().message("这不是本人的分享");

    }

}

